package com.mt.fpb.controller;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.MD5;
import com.mt.fpb.common.config.AuthUserPerssion;
import com.mt.fpb.mapper.*;
import com.mt.fpb.model.TWarning;
import com.mt.fpb.model.User;
import com.mt.fpb.model.vo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户管理控制层
 * autour: hjm
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;


    /*    */

    /**
     * 登录
     * author:hjm
     *
     * @param user
     * @return
     *//*
    @PostMapping("login")
    public CommonResult login(User user) {
        String name = user.getUserName();
        String password = user.getPassword();

        MD5 md5 = SecureUtil.md5();
        String pass = md5.digestHex(password);
        user.setPassword(pass);


        User u = userMapper.getUserByNameAndPassword(user);
        if (!StringUtils.isEmpty(u)) {
            return CommonResult.success(u);
        } else {
            return CommonResult.fail(-1, "登录失败用户不存在");
        }
//        return CommonResult.success(1);
    }*/

    /**
     * 用户登录 LT
     * 2021年6月16日15:04:09
     *
     * @param user
     * @return
     */
    @PostMapping("login")
    public CommonResult login(@RequestBody User user) {
        if (StringUtils.isEmpty(user.getUserName()) && StringUtils.isEmpty(user.getPassword())) {
            return CommonResult.fail(-1, "用户名或密码不能为空");
        }
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("userName", user.getUserName()).andEqualTo("password", SecureUtil.md5().digestHex(user.getPassword()));
        User u = userMapper.selectOneByExample(example);
        if (StringUtils.isEmpty(u)) {
            return CommonResult.success("用户名或密码错误");
        }
        return CommonResult.success(u);
    }

    /**
     * 修改密码
     *
     * @param user
     * @return
     */
    @PostMapping("updatePassword")
    public CommonResult updatePassword(@RequestBody User user) {
        if (StringUtils.isEmpty(user.getOldPassword())) {
            return CommonResult.fail(-1, "旧密码不能为空");
        }
        if (StringUtils.isEmpty(user.getNewPassword())) {
            return CommonResult.fail(-1, "新密码不能为空");
        }
        // 先根据用户id查询到用户的旧密码
        User u = userMapper.getUserByUserName(user);
        if (u.getPassword().equals(user.getOldPassword())) { // 密码校验，相同，则修改密码
            // 将新密码替换掉旧密码
            int i = userMapper.updatePassword(user.getNewPassword(), u.getUserId());
            if (i > 0) { // 修改成功
                return CommonResult.success(1);
            } else { // 修改密码失败
                return CommonResult.fail(-1, "修改密码失败");
            }
        } else { // 不能修改
            return CommonResult.fail(-1, "两次输入的密码不相同，请重新填写!!");
        }
    }


    /**
     * 退出登录
     * author:hjm
     *
     * @param request
     * @return
     */
    @PostMapping("logout")
    public CommonResult logout(HttpServletRequest request) {
        request.getSession();


        return null;
    }

}
